#pragma once

#include <memory>
#include "wxtl/base.h"

struct IocpOperation;
class IocpFiber;

struct IocpResult	
{
	IocpResult(size_t bytes, const wxtl::_win32_error& error, bool fail)
		:bytes(bytes)
		,error(error)
		,fail(fail)
	{

	}
	size_t bytes;
	wxtl::_win32_error error;
	bool fail;
};

/*class AsyncOperation: public IocpOperation
{
	AsyncOperation()
	{

	}
	~AsyncOperation()
	{
		Wait();
	}

public:
	bool IsCompleted();
	void Complete();
	void Wait();

private:
	volatile int m_completed;
};*/

//this class tries to be as abstract iocp event initiator as possible
class BaseIocpInitiator
{
public:
	BaseIocpInitiator(void);
	virtual ~BaseIocpInitiator(void);

	virtual bool ProcessIocpRequest() = 0;
	virtual bool Validate() = 0;
	virtual void Close() = 0;
	virtual void OnOperationCompleted(const IocpResult& result, IocpOperation* operation);

protected:
	IocpOperation* PrepareExOperation();
	virtual void OnSwithcedContext();
	
	void SetResult(const IocpResult& result);
	void Switch();
	void Process( IocpFiber* destination, BaseIocpInitiator* initiator );
private:
	std::auto_ptr<IocpOperation> m_operation;

protected:
	IocpResult m_result;
};

